package com.gugee.ins.common.dal.service.comment.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gugee.ins.common.dal.mapper.comment.InsCommentMapper;
import com.gugee.ins.common.dal.service.comment.InsCommentDao;
import com.gugee.ins.common.model.mysql.comment.InsComment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * insBlogger dao
 * @author Huangxin
 */
@Slf4j
@Service
public class InsCommentDaoImpl extends ServiceImpl<InsCommentMapper,InsComment> implements InsCommentDao {


    /**
     * 批量插入
     * @param insComments
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchInsert(List<InsComment> insComments) {
        try{
            this.saveBatch(insComments,insComments.size() + 1);
        }catch (Exception e){
            log.debug("batch insert ins comment failed ,try insert one by one, msg : {} ",e.getMessage());
            insComments.forEach(insComment -> {
                try{
                    baseMapper.insert(insComment);
                }catch (Exception ex){
                    log.info("insert ins comment failed  ,cid: {} ,shortcode : {},c_uid : {}, msg : {} "
                            ,insComment.getId(),insComment.getShortcode()
                            ,insComment.getCUid(),ex.getMessage());
                }
            });
        }
    }

    /**
     * 删除帖子的评论数据
     * @param shortcode
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByShortcode(String shortcode) {
        Map<String,Object> deleteMap = new HashMap<>(1);
        deleteMap.put("shortcode",shortcode);
        baseMapper.deleteByMap(deleteMap);
    }

    /**
     * 根据 博主uid 查找未分析语言的活跃粉丝的评论
     * @param uid
     * @return
     */
    @Override
    public List<InsComment> findAnalysisLanguageCommentByUid(long uid){
        return baseMapper.findAnalysisLanguageCommentByUid(uid);
    }

}
